package com.jader.adapter;

import java.util.ArrayList;
import java.util.List;

import com.jader.beans.LunTan_ALLHuiFu;
import com.jader.util.JaderApplication;
import com.jader.util.NoScrollListView;
import com.zlp.jader.R;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CommentAdapter extends BaseAdapter {

	private int resourceId;
	private Context context;
	private Handler handler;
	private List<LunTan_ALLHuiFu> list, chlidAllHuiFus;
	private LayoutInflater inflater;
	private ReplyAdapter adapter;

	public CommentAdapter(Context context, List<LunTan_ALLHuiFu> list,
			List<LunTan_ALLHuiFu> allHuiFus, int resourceId, Handler handler) {
		this.list = list;
		this.context = context;
		this.handler = handler;
		this.resourceId = resourceId;
		this.chlidAllHuiFus = allHuiFus;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LunTan_ALLHuiFu bean = list.get(position);
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(resourceId, null);
			holder.commentItemImg = (ImageView) convertView
					.findViewById(R.id.commentItemImg);
			holder.commentNickname = (TextView) convertView
					.findViewById(R.id.commentNickname);
			holder.replyText = (TextView) convertView
					.findViewById(R.id.replyText);
			holder.commentItemTime = (TextView) convertView
					.findViewById(R.id.commentItemTime);
			holder.commentItemContent = (TextView) convertView
					.findViewById(R.id.commentItemContent);
			holder.replyList = (NoScrollListView) convertView
					.findViewById(R.id.replyList);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		JaderApplication.getBitmapUtils().display(holder.commentItemImg,
				JaderApplication.getIP()+"/Jader"+ bean.getUser().getPicture());
		holder.commentNickname.setText(bean.getUser().getUsername());
		holder.commentItemTime.setText(bean.getTime());
		holder.commentItemContent.setText(bean.getContent());
		List<LunTan_ALLHuiFu> child = new ArrayList<LunTan_ALLHuiFu>();
		LunTan_ALLHuiFu huifu = list.get(position);
		for (LunTan_ALLHuiFu lunTan_ALLHuiFu : chlidAllHuiFus) {
			if (huifu.getTiezi_id() == lunTan_ALLHuiFu.getHuifu_id()) {
				child.add(lunTan_ALLHuiFu);
			}
		}
		adapter = new ReplyAdapter(context, child, chlidAllHuiFus, bean
				.getUser().getUsername(), handler);
		holder.replyList.setAdapter(adapter);
		TextviewClickListener tcl = new TextviewClickListener(position);
		holder.replyText.setOnClickListener(tcl);

		return convertView;
	}

	private final class ViewHolder {
		public ImageView commentItemImg; // 评论人图片
		public TextView commentNickname; // 评论人昵称
		public TextView replyText; // 回复
		public TextView commentItemTime; // 评论时间
		public TextView commentItemContent; // 评论内容
		public NoScrollListView replyList; // 评论回复列表
	}

	/**
	 * 事件点击监听器
	 */
	private final class TextviewClickListener implements OnClickListener {
		private int position;

		public TextviewClickListener(int position) {
			this.position = position;
		}

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.replyText:
				Message message = new Message();
				message.what = 10;
				message.obj = list.get(position);
				handler.sendMessage(message);
				break;
			}
		}
	}

	public void add(LunTan_ALLHuiFu allHuiFu) {
		chlidAllHuiFus.add(allHuiFu);

	}

	@Override
	public void notifyDataSetChanged() {
		if (adapter != null) {
			adapter.notifyDataSetChanged();
		}
		super.notifyDataSetChanged();
	}

}
